package sia.erp.venta_ruta;

import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;
import java.util.concurrent.ExecutionException;

import android.app.Activity;
import android.database.DataSetObserver;
import android.graphics.Color;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Spinner;
import android.widget.SpinnerAdapter;
import android.widget.TextView;

public class ConsultaUltimaVenta extends Activity {

	List<String> clientes;
	List<String> articulos;
	Spinner clientesSpinner;
	Spinner articulosSpinner;
	TextView fechaEntrega;
	TextView totalVenta;
	TextView totalVentaAnyo;
	TextView promedio;
	TextView stock;
	TextView solicitado;
	TextView disponible;
	TextView comprometido;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_consulta_ultima_venta);
		
		clientesSpinner = (Spinner) findViewById(R.id.clientes_spinner_consulta3);
		articulosSpinner = (Spinner) findViewById(R.id.articulos_spinner_consulta3);
		fechaEntrega = (TextView) findViewById(R.id.fecha_entrega_consulta2);
		totalVenta = (TextView) findViewById(R.id.total_venta_consulta2);
		totalVentaAnyo = (TextView) findViewById(R.id.total_ventas_anyo_consulta2);
		promedio = (TextView) findViewById(R.id.promedio_facturas_consulta2);
		stock = (TextView) findViewById(R.id.stock_consulta2);
		solicitado = (TextView) findViewById(R.id.solicitado_consulta2);
		disponible = (TextView) findViewById(R.id.disponible_consulta2);
		comprometido = (TextView) findViewById(R.id.comprometido_consulta2);
		
		try {
			clientes = Utils.consultar(this, 6);
			articulos = Utils.consultar(getApplicationContext(), 4);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ExecutionException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		ArrayListSpinnerAdapter adapterClientes = new ArrayListSpinnerAdapter(clientes);
		ArrayListSpinnerAdapter adapterArticulos = new ArrayListSpinnerAdapter(articulos);
		clientesSpinner.setAdapter(adapterClientes);
		articulosSpinner.setAdapter(adapterArticulos);
		
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.activity_consulta_ultima_venta, menu);
		return true;
	}
	
	public void consultar(View view){
		List<String> consultaResponse = new ArrayList<String>();
		String cliente = clientesSpinner.getSelectedItem().toString();
		String articulo = articulosSpinner.getSelectedItem().toString();
		try {
			consultaResponse = Utils.ConsultarEmpresaCliente(this, cliente, articulo, 2);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ExecutionException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		if(!consultaResponse.isEmpty()){
			
			fechaEntrega.setText(consultaResponse.get(0));
			totalVenta.setText(consultaResponse.get(1));
			totalVentaAnyo.setText(consultaResponse.get(2));
			promedio.setText(consultaResponse.get(3));
			StringTokenizer tokenizer = new StringTokenizer(consultaResponse.get(4), ";");
			stock.setText(tokenizer.nextToken());
			solicitado.setText(tokenizer.nextToken());
			disponible.setText(tokenizer.nextToken());
			comprometido.setText(tokenizer.nextToken());
		}
		
	}
	
	private class ArrayListSpinnerAdapter implements SpinnerAdapter {

		List<String> data;
		
		public ArrayListSpinnerAdapter(List<String> data){
			this.data = data;
		}
		
		@Override
		public int getCount() {
			return data.size();
		}

		@Override
		public Object getItem(int arg0) {
			return data.get(arg0);
		}

		@Override
		public long getItemId(int arg0) {
			return arg0;
		}

		@Override
		public int getItemViewType(int arg0) {
			return android.R.layout.simple_spinner_dropdown_item;
		}

		@Override
		public View getView(int arg0, View arg1, ViewGroup arg2) {
			TextView v = new TextView(getApplicationContext());
			v.setTextColor(Color.BLACK);
			v.setText(data.get(arg0));
			return v;
		}

		@Override
		public int getViewTypeCount() {
			return 1;
		}

		@Override
		public boolean hasStableIds() {
			// TODO Auto-generated method stub
			return false;
		}

		@Override
		public boolean isEmpty() {
			// TODO Auto-generated method stub
			return false;
		}

		@Override
		public void registerDataSetObserver(DataSetObserver arg0) {
			// TODO Auto-generated method stub

		}

		@Override
		public void unregisterDataSetObserver(DataSetObserver arg0) {
			// TODO Auto-generated method stub

		}

		@Override
		public View getDropDownView(int arg0, View arg1, ViewGroup arg2) {
			return this.getView(arg0, arg1, arg2);
		}

	}

}
